package net.lcheng.service.impl;

import net.lcheng.dao.SysRolePermissionsMapper;
import net.lcheng.dao.SysRolesMapper;
import net.lcheng.model.SysRolePermissions;
import net.lcheng.model.SysRolePermissionsExample;
import net.lcheng.model.SysRoles;
import net.lcheng.model.SysRolesExample;
import net.lcheng.service.SysRolesService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by qiliping on 15/12/16.
 * 系统角色
 */
@Service("SysRolesService")
public class SysRolesServiceImpl implements SysRolesService {
    @Resource
    private SysRolesMapper mapper;
    @Resource
    private SysRolePermissionsMapper rolePermissionsMapper;

    @Override
    public SysRoles getRoleById(int id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SysRoles> getRoleList() {
        SysRolesExample example = new SysRolesExample();

        return mapper.selectByExample(example);
    }
    /**
     * 添加角色
     * */
    @Override
    public int addRole(String roleName, String roleKey, String[] rolePermissionKeys) {
        SysRoles roles = new SysRoles();
        roles.setCreateOn(new Date());
        roles.setRoleKey(roleKey);
        roles.setRoleName(roleName);
        mapper.insert(roles);
        int roleId = 0;
        //去数据库查询roleId
        SysRolesExample example= new SysRolesExample();
        example.or().andRoleKeyEqualTo(roleKey);
        List<SysRoles> rolesList = mapper.selectByExample(example);
        if(rolesList!=null&&rolesList.size()==1){
            roleId = rolesList.get(0).getSysRoleId();
            for (String rpk :rolePermissionKeys) {
                SysRolePermissions pModel = new SysRolePermissions();
                pModel.setSysRoleId(roleId);
                pModel.setRoleKey(roleKey);
                pModel.setSysPermissionId(0);
                pModel.setPermissionKey(rpk);
                rolePermissionsMapper.insert(pModel);
            }
        }

        return roleId;
    }

    @Override
    public int editRole(int roleId, String roleName, String roleKey, String[] rolePermissionKeys) {
        SysRoles roles = new SysRoles();
        roles.setSysRoleId(roleId);
        roles.setRoleKey(roleKey);
        roles.setRoleName(roleName);
        //更新角色
        mapper.updateByPrimaryKeySelective(roles);

        //删除旧权限
        SysRolePermissionsExample example = new SysRolePermissionsExample();
        example.or().andSysRoleIdEqualTo(roleId);
        rolePermissionsMapper.deleteByExample(example);

        //添加新权限
        for (String rpk :rolePermissionKeys) {
            SysRolePermissions srp = new SysRolePermissions();
            srp.setSysRoleId(roleId);
            srp.setRoleKey(roleKey);
            srp.setSysPermissionId(0);
            srp.setPermissionKey(rpk);
            rolePermissionsMapper.insert(srp);
        }

        return 1;
    }
    /**
     * 删除角色
     * */
    @Override
    public int deleteRole(int roleId) {
        //删除权限
        SysRolePermissionsExample example = new SysRolePermissionsExample();
        example.or().andSysRoleIdEqualTo(roleId);
        rolePermissionsMapper.deleteByExample(example);

        //删除角色
        mapper.deleteByPrimaryKey(roleId);
        return 1;
    }
}
